//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Microsoft.ApplicationBlocks.Mobile.Updater.Evaluators
{
    public class DeviceStatusNativeHelper
    {
        [DllImport("coredll.dll")]
        private static extern bool GetDiskFreeSpaceEx(string directoryName,
            out long freeBytesAvailable,
            out long totalBytes,
            out long totalFreeBytes);


        [DllImport("note_prj.dll")]
        private static extern IntPtr FindFirstFlashCard(ref WIN32_FIND_DATA lpFindFlashData);

        [DllImport("note_prj.dll")]
        private static extern bool FindNextFlashCard(IntPtr handle, ref WIN32_FIND_DATA lpFindFlashData);

        [DllImport("coredll.dll")]
        private static extern bool FindClose(IntPtr handle);

        [StructLayout(LayoutKind.Sequential)]
        public struct FILETIME
        {
            public int dwLowDateTime;
            public int dwHighDateTime;
        };

        [StructLayout(LayoutKind.Sequential)]
        public struct WIN32_FIND_DATA
        {
            public int dwFileAttributes;
            public FILETIME ftCreationTime;
            public FILETIME ftLastAccessTime;
            public FILETIME ftLastWriteTime;
            public int nFileSizeHigh;
            public int nFileSizeLow;
            public int dwOID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public String cFileName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
            public String cAltFileName;
        };

        public static double GetFreeBytes(String path)
        {
            long freeBytesAvailable;
            long totalBytes;
            long totalFreeBytes;

            GetDiskFreeSpaceEx(path, out freeBytesAvailable, out totalBytes, out totalFreeBytes);

            return (double)freeBytesAvailable;
        }

        public static List<String> GetStorageCardNames()
        {
            List<String> storageCards = new List<string>();

            WIN32_FIND_DATA data = new WIN32_FIND_DATA();

            IntPtr handle = FindFirstFlashCard(ref data);

            do
            {
                if (!String.IsNullOrEmpty(data.cFileName))
                    storageCards.Add(data.cFileName);
            }
            while (FindNextFlashCard(handle, ref data));

            FindClose(handle);

            return storageCards;
        }
    }
}
